//
//  SecondTabHotCollectionViewCell.m
//  项目A --半糖
//
//  Created by romana on 16/6/1.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "SecondTabHotCollectionViewCell.h"
#import "SecondTabHotModel.h"
#import "DataBaseManager.h"

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation SecondTabHotCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backImgView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.backImgView];
        self.backImgView.layer.borderWidth = 1;
        self.backImgView.layer.borderColor = [[UIColor brownColor]CGColor];
        self.backImgView.layer.cornerRadius = 5;
        self.imgView = [[UIImageView alloc] init];
        [self.backImgView addSubview:self.imgView];
        self.nameLable = [[UILabel alloc] init];
        [self.backImgView addSubview:self.nameLable];;
        self.priceLable = [[UILabel alloc] init];
        self.priceLable.textColor = [UIColor redColor];
        [self.backImgView addSubview:self.priceLable];
        self.nameLable.font = [UIFont systemFontOfSize:14];
        self.nameLable.numberOfLines = 2;
        self.imgView.backgroundColor = [UIColor clearColor];
        self.nameLable.backgroundColor = [UIColor clearColor];
        self.priceLable.backgroundColor = [UIColor clearColor];
        
        self.likeImgView = [[UIImageView alloc] init];
        self.likeImgView.userInteractionEnabled = YES;
        [self.contentView addSubview:self.likeImgView];
        self.likeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self.likeImgView addSubview:self.likeBtn];
        [self.likeBtn addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.likeBtn setImage:[UIImage imageNamed:@"iconfont-shoucang"] forState:(UIControlStateNormal)];
        [self.likeBtn setImage:[UIImage imageNamed:@"iconfont-shoucang1"] forState:(UIControlStateSelected)];
        self.likeLable = [[UILabel alloc] init];
        [self.likeImgView addSubview:self.likeLable];
        self.likeLable.font = [UIFont systemFontOfSize:10];
        
        //        self.likeLable.backgroundColor = [UIColor yellowColor];
        //        self.likeImgView.backgroundColor = [UIColor redColor];
        
    }
    return self;
}
-(void)click:(UIButton *)btn{
    if (btn.selected) {
        [[DataBaseManager shareIndtance] deleteHotByTitle: self.nameLable.text];
        
    }else{
        //        insertHotTitle:self.nameLable.text imgView:self.imgView.image WebView:self.url
        [[DataBaseManager shareIndtance] insertHotTitle:self.nameLable.text imgView:self.imgView.image WebView:self.url type:self.type];
    }
    // 点击按钮后的收藏动画
    btn.selected = !btn.selected;
    CAKeyframeAnimation *clickAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    clickAnimation.values = @[@(0.1),@(1.0),@(1.5)];
    clickAnimation.duration = 0.2;
    [btn.layer addAnimation:clickAnimation forKey:@"key"];
    
}


-(void)layoutSubviews{

    [super layoutSubviews];
    self.backImgView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.imgView.frame = CGRectMake(1, 0, self.contentView.frame.size.width - 2, HEIGHT * 7 / 11);
    self.nameLable.frame = CGRectMake(WIDTH / 12, HEIGHT * 2 / 3 , WIDTH * 10 / 12 + 10, 40);
    self.priceLable.frame = CGRectMake(WIDTH / 12, HEIGHT - HEIGHT / 6 + 5, WIDTH / 3, 25);
    self.likeImgView.frame = CGRectMake(WIDTH - 80, HEIGHT - HEIGHT / 6 + 5, 70, 25);
    self.likeBtn.frame = CGRectMake(0, 0, 30, 25);
    self.likeLable.frame = CGRectMake(self.likeBtn.frame.size.width, 0, self.likeImgView.frame.size.width - self.likeBtn.frame.size.width, 25);
    




}


@end
